jQuery ->
  $("#event_title").focus()
  $eventTable = $('#events').dataTable
    order: [[3, 'asc']],
    columnDefs: [
                  { orderable: false, targets: -1 },
                  { orderData: 2, targets: 3 },
                  { orderData: 4, targets: 5 },
                  { visible: false, targets: [2,4] }
                ]

#logic handling checkbox filtering with dataTables()
  $.fn.dataTableExt.afnFiltering.push((settings, data) ->
    # add table ID to array if you would like it to be included in filtering
    allowedTables = ['events'];
    if ( $.inArray( settings.nTable.getAttribute('id'), allowedTables ) == -1 )
       # if not table should be ignored
       return true;
    oneYearAgo = new Date()
    # todays date minus 396 days
    oneYearAgo.setDate(oneYearAgo.getDate() - 396)
    checked = []
    startTime = new Date(data[3])
    status = data[7]
    $('.event-filter').each ->
      $this = $(this)
      if $this.is(':checked')
        checked.push($this)
    if checked.length
      returnValue = false
      $.each(checked, (i, element) ->
        if element.val() is 'current' and (status is "In-session" or status is "Scheduled") or
           element.val() is 'recent' and (startTime > oneYearAgo)
          returnValue = true;
          return false;
          )
    else
      return false
    return returnValue
    )

  #re-draws table after checking or unchecking a filter checkbox
  $('.event-filter').change ->
    $eventTable.fnDraw()

  # training events should display course info
  $(window).load ->
    edit_event_cat = $('.edit_event #event_category option:selected').text()
    if edit_event_cat is 'Training'
      $(".training-controls").css("display", "block")

  $("#event_category").change ->
    event_cat = $("#event_category option:selected").text();
    if event_cat is "Training"
      $(".training-controls").css("display", "block")
    else
      $(".training-controls").css("display", "none")

  $("#event_status").change ->
    status = $("#event_status option:selected").text();
    start_time = $("#event_start_time").text();
    if status is "Completed"
      $("#XXevent_start_time").hide()

  $('a[data-toggle="tab"]').on 'shown.bs.tab', (e) ->
    $.fn.dataTable.tables(
      visible: true
      api: true).columns.adjust()
    return

  $('#event_start_time').change ->
    eventStartTime = $('#event_start_time').datetimepicker('getValue')
    eventEndTime = $('#event_end_time').datetimepicker('getValue')
    if (not eventEndTime?) or (eventStartTime > eventEndTime)
      $('#event_end_time').datetimepicker('setOptions', {startDate: eventStartTime})

$(document).ready ->
  $("#js-events-current-checkbox").prop('checked', false)
  $("#js-events-current-checkbox").click()
  
